package com.wanglibao.woodtrade.woodtrade.ui.fragment.adapter;

import android.app.Activity;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.wanglibao.woodtrade.woodtrade.R;
import com.wanglibao.woodtrade.woodtrade.util.CommonUtil;

import org.xutils.image.ImageOptions;
import org.xutils.x;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by ybli on 2015/8/11.
 */
public class HomepageBannerViewPager {

	List<View> dots = new ArrayList<>();
	private List<View> homepageAdvertisementPicList;
	private int mainImgoldPosition = 0;// 记录上一次点的位置

	private ViewPager viewpager_container;
	private ViewPagerAdapter mainViewPagerAdapter;

	private Activity  mContext;

	private LinearLayout dotLayout;

	private List<String> imageUrls;

	private int imageSize = 0;

	int state_code;

//	ScrollView scrollView;

	public HomepageBannerViewPager(Activity context, ViewPager viewpager_container, List<String> imageUrls) {
		this.mContext = context;
		this.viewpager_container = viewpager_container;
		this.imageUrls = imageUrls;
	}

	public HomepageBannerViewPager(Activity context, ViewPager viewpager_container, List<String> imageUrls, LinearLayout linearLayout) {
		this.mContext = context;
		this.viewpager_container = viewpager_container;
		this.imageUrls = imageUrls;
		this.dotLayout = linearLayout;
	}
	
	// 初始化轮播图片

	public void initScrollView() {
		homepageAdvertisementPicList = new ArrayList<View>();
		imageSize = imageUrls == null ? 0 : imageUrls.size();
		LinearLayout.LayoutParams dotLayoutParams = new LinearLayout.LayoutParams(CommonUtil.dip2px(mContext,7),CommonUtil.dip2px(mContext,7));
		dotLayoutParams.setMargins(0,0,CommonUtil.dip2px(mContext,5),0);

		LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
				LinearLayout.LayoutParams.MATCH_PARENT);

		ImageOptions imageOptions = new ImageOptions.Builder().setLoadingDrawable(mContext.getResources().getDrawable(R.mipmap.ic_lose_p_loading))
				.setFailureDrawable(mContext.getResources().getDrawable(R.mipmap.ic_lose_p_fail)).setUseMemCache(true).build();
		for (int i = 0; i < imageSize; i++){
			ImageView img = new ImageView(mContext);
			img.setLayoutParams(layoutParams);
			x.image().bind(img,imageUrls.get(i),imageOptions);
			homepageAdvertisementPicList.add(img);
			if (dotLayout != null){
				View view = View.inflate(mContext,R.layout.view_dot,null);
				dots.add(view);
				dotLayout.addView(view);
				view.setLayoutParams(dotLayoutParams);
			}

		}
		if (dotLayout != null && dots.size() > 1){
			dots.get(0).setBackgroundResource(R.drawable.dot_focused);
		}
		// 图片轮播的viewpager
		mainViewPagerAdapter = new ViewPagerAdapter();
		viewpager_container.setAdapter(mainViewPagerAdapter);
		viewpager_container.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
			@Override
			public void onPageSelected(int position) {
				if (dotLayout != null){
					dots.get(mainImgoldPosition).setBackgroundResource(R.drawable.dot_normal);
					dots.get(position).setBackgroundResource(R.drawable.dot_focused);
				}
				mainImgoldPosition = position;
			}
			@Override
			public void onPageScrolled(int arg0, float arg1, int arg2) {
			}
			@Override
			public void onPageScrollStateChanged(int arg0) {
			}
		});

//		if (dotLayout != null){
//			// 图片自动播放所延迟的时间
//			Timer timer = new Timer();
//			TimerTask task = new TimerTask() {
//				@Override
//				public void run() {
//					Message msg = new Message();
//					msg.what = 0;
//					handler.sendMessage(msg);
//				}
//			};
//			// 重复播放 每隔两秒 发送
//			timer.schedule(task, 4000, 3000);
//		}
	}



	// 加载视图的Adapter
	private class ViewPagerAdapter extends PagerAdapter {

		@Override
		public int getCount() {
			return homepageAdvertisementPicList.size();
		}

		// 是否是同一张图片
		@Override
		public boolean isViewFromObject(View arg0, Object arg1) {
			return arg0 == arg1;
		}

		// 移除
		@Override
		public void destroyItem(ViewGroup view, int position, Object object) {
			view.removeView(homepageAdvertisementPicList.get(position));
		}

		// 添加
		@Override
		public Object instantiateItem(ViewGroup view, int position) {
			view.addView(homepageAdvertisementPicList.get(position));
			return homepageAdvertisementPicList.get(position);
		}
	}

//	private Handler handler = new Handler() {
//		@Override
//		public void handleMessage(Message msg) {
//			Rect scrollBounds = new Rect();
//			scrollView.getHitRect(scrollBounds);
//			if (viewpager_container.getLocalVisibleRect(scrollBounds)) {
//				state_code = 0;
//			} else {
//				state_code = 1;
//			}
//			// 循环滚动
//			if (msg.what == 0 && state_code == 0) {
//				// 设置当前页面 代表最后一张图片
//				if (viewpager_container.getCurrentItem() == homepageAdvertisementPicList.size() - 1) {
//					// 指的是不平滑过度
//					viewpager_container.setCurrentItem(0, false);
//				} else {
//					// 默认为平滑过度
//					viewpager_container.setCurrentItem(viewpager_container.getCurrentItem() + 1);
//				}
//			}
//			super.handleMessage(msg);
//		}
//	};

}
